//
//  XQCourseDetailFirstTableViewCell.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/29.
//  Copyright © 2016年 校企桥. All rights reserved.
//

import UIKit

class XQCourseDetailFirstTableViewCell: UITableViewCell {

    var icon = UIImageView()
    var nameLabel = UILabel()
    var introlabel = UILabel()
    
    var textView = UITextView()
    var line3 = UIView()
    
    var homeself = XQCourseDetailViewController()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        InitUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var csMode:XQCourseDetailCSMode?{
        didSet{
            
            
            let htmlText = csMode?.course_intro
            
            do{
                let attrStr = try NSAttributedString(data: (htmlText?.data(using: String.Encoding.unicode, allowLossyConversion: true)!)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
                
                textView.attributedText = attrStr
                
                
            }catch let error as NSError {
                print(error.localizedDescription)
                textView.text = "暂无简介"
            }
            
        }
    }
    
    var msMode:XQCourseDetailMSMode?{
        didSet{
            let url = URL.init(string: (msMode?.avatar)!)
            icon.kf.setImage(with: url)
            nameLabel.text = msMode?.user_role
            introlabel.text = msMode?.pspace
        }
    }
    
    
    
    func InitUI() {
        
        icon = UIImageView.init(frame: CGRect.zero)
        icon.layer.cornerRadius = 25
        icon.layer.borderWidth = 0.5
        icon.layer.borderColor = UIColor.lightGray.cgColor
        self.contentView .addSubview(icon)
        
        nameLabel = UILabel.init(frame: CGRect.zero)
        self.contentView.addSubview(nameLabel)
        
        
        introlabel = UILabel.init(frame: CGRect.zero)
        introlabel.numberOfLines = 0
        self.contentView.addSubview(introlabel)
        
        let button1 = UIButton.init(frame: CGRect.zero)
        button1 .setTitle("个人主页 >", for: .normal)
        button1.setTitleColor(UIColor.gray, for: .normal)
        button1.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        button1.backgroundColor = UIColor.groupTableViewBackground
        button1.layer.cornerRadius = 3;
        self.contentView .addSubview(button1)
        
        let button2 = UIButton.init(frame: CGRect.zero)
        button2.setTitle("简介", for: .normal)
        button2.setTitleColor(UIColor.black, for: .normal)
        self.contentView .addSubview(button2)
    
        let button3 = UIButton.init(frame: CGRect.zero)
        button3.setTitle("提纲", for: .normal)
        button3.setTitleColor(UIColor.black, for: .normal)
        self.contentView .addSubview(button3)

        textView = UITextView.init(frame: CGRect.zero)
        textView.isEditable = false
        self.contentView.addSubview(textView)
        
        button2 .addTarget(self, action: #selector(XQCourseDetailFirstTableViewCell.changeAction), for: .touchUpInside)
        button3 .addTarget(self, action: #selector(XQCourseDetailFirstTableViewCell.changeAction), for: .touchUpInside)

      
        

        
        icon.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView.snp.left).offset(10)
            make.top.equalTo(self.contentView.snp.top).offset(10)
            make.width.equalTo(50)
            make.height.equalTo(50)
        }
        
        button1.snp.makeConstraints { (make) in
            make.right.equalTo(self.contentView.snp.right).offset(-10)
            make.top.equalTo(self.contentView.snp.top).offset(10)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(icon.snp.right).offset(5)
            make.bottom.equalTo(icon.snp.centerY)
           // make.right.equalTo(button1.snp.left).offset(-5)
        }
        introlabel.snp.makeConstraints { (make) in
            make.left.equalTo(icon.snp.right).offset(5)
            make.top.equalTo(icon.snp.centerY)
            //make.right.equalTo(button1.snp.left).offset(-5)
        }
        
        
        button2.snp.makeConstraints { (make) in
            make.top.equalTo(icon.snp.bottom).offset(15)
            make.left.equalTo(self.contentView.snp.left)
            make.right.equalTo(self.contentView.snp.centerX)
            
        }
        
        let line1 = UIView.init(frame: CGRect.zero)
        line1.backgroundColor = UIColor.black
        self.contentView.addSubview(line1)
        
        line1.snp.makeConstraints { (make) in
            make.width.equalTo(1)
            make.centerX.equalTo(self.contentView.snp.centerX)
            make.centerY.equalTo(button2.snp.centerY)
            make.height.equalTo(15)
        }
        
        button3.snp.makeConstraints { (make) in
            make.top.equalTo(icon.snp.bottom).offset(15)
            make.right.equalTo(self.contentView.snp.right)
            make.left.equalTo(self.contentView.snp.centerX)
        }
        
        let line2 = UIView.init(frame: CGRect.zero)
        line2.backgroundColor = UIColor.lightGray
        self.contentView.addSubview(line2)
        
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView.snp.left)
            make.right.equalTo(self.contentView.snp.right)
            make.top.equalTo(button2.snp.bottom)
            make.height.equalTo(1)
        }
        
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(button2.snp.bottom).offset(2)
            make.left.equalTo(self.contentView.snp.left)
            make.right.equalTo(self.contentView.snp.right)
            make.height.equalTo(200)
        }
        
        line3 = UIView.init(frame: CGRect.zero)
        line3.backgroundColor = UIColor.green
        self.contentView.addSubview(line3)
        
        line3.snp.makeConstraints { (make) in
            make.width.equalTo(SCREEN_WIDTH/2)
            make.height.equalTo(1)
            make.centerY.equalTo(line2.snp.centerY)
            make.centerX.equalTo(button2.snp.centerX)
        }
    
        line3.center.equalTo(button2.center)
        
        
    }
    
    func changeAction(sender:UIButton) {
        line3.center.x = sender.center.x
        
    }
    

}
